<template>
  <div>
    <Header />

    <div class="register-container">
      <!-- 注册内容 -->
      <div class="register">
        <h3>华为账号注册</h3>
        <div class="login-link">
          <span class="go"
            >我有账号，去 <a href="/login" target="_blank">登录></a>
          </span>
        </div>
        <ValidationObserver v-slot="{ handleSubmit }">
          <form @submit.prevent="handleSubmit(register)">
            <!-- 手机号 -->
            <ValidationProvider
              class="content"
              name="phone"
              rules="phoneRequired|phone"
              v-slot="{ errors }"
              tag="div"
              mode="lazy"
            >
              <label>手机号:</label>
              <input type="text" placeholder="手机号" v-model="user.phone" />
              <span class="error-msg">{{ errors[0] }}</span>
            </ValidationProvider>

            <!-- 验证码 -->
            <ValidationProvider
              class="content"
              name="code"
              rules="codeRequired|code"
              v-slot="{ errors }"
              tag="div"
              mode="lazy"
            >
              <label>验证码:</label>
              <input type="text" placeholder="验证码" v-model="user.code" />
              <span v-if="time <= 60">{{ time }} 秒后重新发送 </span>
              <button class="getcode" @click="sendCode" v-else>
                获取验证码
              </button>
              <span class="error-msg">{{ errors[0] }}</span>
            </ValidationProvider>

            <!-- 密码 -->
            <ValidationProvider
              class="content"
              name="password"
              rules="passwordRequired|password"
              v-slot="{ errors }"
              tag="div"
              mode="lazy"
            >
              <label>密码:</label>
              <input type="text" placeholder="密码" v-model="user.password" />
              <span class="error-msg">{{ errors[0] }}</span>
            </ValidationProvider>

            <!-- 确认密码 -->
            <ValidationProvider
              class="content"
              name="rePassword"
              :rules="`rePasswordRequired|rePassword:${user.password}`"
              v-slot="{ errors }"
              tag="div"
              mode="lazy"
            >
              <label>确认密码:</label>
              <input
                type="text"
                placeholder="确认密码"
                v-model="user.rePassword"
              />
              <span class="error-msg">{{ errors[0] }}</span>
            </ValidationProvider>

            <div class="btn">
              <button>完成注册</button>
            </div>
          </form>
        </ValidationObserver>
      </div>
    </div>
    <Footer />
  </div>
</template>

<script>
import Header from "@/components/Header-lgreg";
import Footer from "@/components/Footer-lgreg";
import { ValidationObserver, ValidationProvider, extend } from "vee-validate";
import { required } from "vee-validate/dist/rules";
import { reqSendCode, reqRegister } from "@/api/user";
import { phoneReg } from "@/utils/http/regs";

// 定义表单校验规则
// extend("phoneRequired", {
//   ...required,
//   message: "请输入手机号",
// });

// const phoneReg = /^1[3-9][0-9]{9}$/;

// extend("phone", {
//   validate(phone) {
//     return phoneReg.test(phone);
//   },
//   message: "手机号不符合规范",
// });

extend("codeRequired", {
  ...required,
  message: "请输入验证码",
});

const codeReg = /^[0-9]{6}$/;

extend("code", {
  validate(code) {
    return codeReg.test(code);
  },
  message: "验证码不符合规范",
});

// extend("passwordRequired", {
//   ...required,
//   message: "请输入密码",
// });

// const passwordReg = /^[A-Za-z0-9_]{6,18}$/;

// extend("password", {
//   validate(password) {
//     return passwordReg.test(password);
//   },
//   message: "密码不符合规范",
// });

extend("rePasswordRequired", {
  ...required,
  message: "请输入确认密码",
});

extend("rePassword", {
  validate(rePassword, { password }) {
    return rePassword === password;
  },
  params: ["password"],
  message: "密码不一致",
});
export default {
  name: "Register",
  components: {
    Header,
    Footer,
    ValidationObserver,
    ValidationProvider,
  },
  data() {
    return {
      user: {
        phone: "",
        code: "",
        password: "",
        rePassword: "",
      },
      time: 61,
    };
  },

  methods: {
    async register() {
      const { phone, code, password } = this.user;
      await reqRegister({ phone, code, password });
      this.$router.push("/login");
    },
    // 发送验证码
    async sendCode() {
      const phone = this.user.phone;
      if (!phoneReg.test(phone)) return;

      this.time--;

      this.timeId = setInterval(() => {
        this.time--;
        if (this.time <= 0) {
          this.time = 61;
          clearInterval(this.timeId);
        }
      }, 1000);

      // 发送请求
      const code = await reqSendCode(phone);
      console.log(code);
    },
  },
  beforeDestroy() {
    clearInterval(this.timeId);
  },
};
</script>

<style lang="less" scoped>
.register-container {
  .register {
    width: 1200px;
    height: 445px;
    margin: 0 auto;

    h3 {
      color: #333;
      text-align: center;
      margin-top: 50px;
      font-size: 25.6px;
    }
    .login-link {
      margin-top: 5px;
      text-align: center;
      font-size: 12px;
      span {
        font-size: 14px;
        display: block;
        padding: 10px 0;

        a {
          color: #5293ff;
        }
      }
    }

    div:nth-of-type(1) {
      margin-top: 40px;
    }

    .content {
      padding-left: 390px;
      margin-bottom: 18px;
      position: relative;

      label {
        font-size: 14px;
        width: 96px;
        text-align: right;
        display: inline-block;
      }

      input {
        width: 270px;
        height: 38px;
        padding-left: 8px;
        box-sizing: border-box;
        margin-left: 5px;
        outline: none;
        background: #f1f1f1;
        opacity: 0.5;
        border-radius: 5px;
      }

      img {
        vertical-align: sub;
      }
    }

    .controls {
      text-align: center;
      position: relative;

      input {
        vertical-align: middle;
      }

      .error-msg {
        position: absolute;
        top: 100%;
        left: 495px;
        color: red;
      }
    }

    .btn {
      text-align: center;
      line-height: 36px;
      margin: 17px 0 0 55px;

      button {
        outline: none;
        width: 200px;
        height: 36px;
        background: #e1251b;
        opacity: 0.5;
        color: #fff !important;
        display: inline-block;
        font-size: 16px;
        border-radius: 5px;
      }
    }

    .getcode {
      height: 38px;
      margin-left: 10px;
      padding: 5px;
      color: #5293ff;
    }
  }
}
</style>
